package cn.autumnorange.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

// WebSecurityConfigurerAdapter如果配置为ResourceServerConfigurerAdapter
// 则访问service-hi 不需要权限的资源依旧会被拦截下来进行权限验证
// 配置为WebSecurityConfigurerAdapter zuul则会顺利通过feign层再转发到资源层
// zuul 详细filter配置 https://blog.csdn.net/wo18237095579/article/details/83543592
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
// @EnableDiscoveryClient
@EnableOAuth2Client
@EnableRedisHttpSession(
    maxInactiveIntervalInSeconds = 7200,
    redisNamespace = "spring:session{springSessionHashTag}") // RedisHttpSession过期时间(秒)
public class ZuulApplication {

  public static void main(String[] args) {
    SpringApplication.run(ZuulApplication.class, args);
  }

  //  private CorsConfiguration buildConfig() {
  //    CorsConfiguration corsConfiguration = new CorsConfiguration();
  //    corsConfiguration.addAllowedOrigin("*"); // 1
  //    corsConfiguration.addAllowedHeader("*"); // 2
  //    corsConfiguration.setAllowCredentials(true);
  //    corsConfiguration.addAllowedMethod("*"); // 3
  //    return corsConfiguration;
  //  }
  //
  //  @Bean
  //  public CorsFilter corsFilter() {
  //    org.springframework.web.cors.UrlBasedCorsConfigurationSource source = new
  // org.springframework.web.cors.UrlBasedCorsConfigurationSource();
  //    source.registerCorsConfiguration("/**", buildConfig()); // 4
  //    return new CorsFilter(source);
  //  }

  //  /**
  //   * 注册check token服务
  //   *
  //   * @param details
  //   * @return
  //   */
  //  @Bean
  //  public RemoteTokenServices tokenService(OAuth2ProtectedResourceDetails details) {
  //    RemoteTokenServices tokenService = new RemoteTokenServices();
  //    tokenService.setCheckTokenEndpointUrl(checkTokenUrl);
  //    tokenService.setClientId(details.getClientId());
  //    tokenService.setClientSecret(details.getClientSecret());
  //    return tokenService;
  //  }

  //    public OAuth2ClientAuthenticationProcessingFilter
  // JsonOAuth2ClientAuthenticationProcessingFilter() {
  //        OAuth2ClientAuthenticationProcessingFilter filter = new
  // JsonOAuth2ClientAuthenticationProcessingFilter("callBack");
  ////    filter.setRestTemplate(oauth2RestTemplate);
  ////    filter.setTokenServices(to
  //        return null;kenService);
  //  //
  //    }
  // 源码ZuulServerAutoConfiguration SendErrorFilter BasicErrorController

  //  @Bean
  //  public SendErrorFilter sendErrorFilter() {
  //    return new GateWayErrorFilter();
  //  }
  //    跨域配置
  @Bean
  public CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(Arrays.asList("http://localhost:8082","http://localhost:3000","http://localhost:3002"));
    configuration.setAllowedMethods(Arrays.asList("*"));
    configuration.addAllowedHeader("*");
    configuration.setAllowCredentials(true);

    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
  }
}
